package com.quectel.business.common.grid;

import cn.hutool.core.lang.func.LambdaUtil;
import com.quectel.constant.core.grid.GridConstant;
import com.quectel.constant.core.household.HouseholdConstants;
import com.quectel.constant.global.SystemConstants;
import com.quectel.core.module.grid.dto.GridBindingDto;
import com.quectel.core.module.grid.dto.GridDto;
import com.quectel.core.module.grid.dto.GridMemberDto;
import com.quectel.core.module.grid.dto.GridMemberGridBindingDto;
import com.quectel.core.module.grid.service.GridBindingService;
import com.quectel.core.module.grid.service.GridMemberGridBindingService;
import com.quectel.core.module.grid.service.GridMemberService;
import com.quectel.core.module.grid.service.GridService;
import com.quectel.core.module.household.dto.HouseholdFamilyDto;
import com.quectel.core.module.household.service.HouseholdService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 在这里编写说明
 * 根据网格员查询网格员绑定的楼栋信息列表 / 实有单位信息列表
 * 返回List<Long> resourceList </>
 *
 * @author: bob
 * @email: bob.yu@quectel.com
 * @date: 2023-02-15 13:56:00
 */
@Component
public class GridMemberBindResourcesBusiness {

    @DubboReference
    private GridService gridService;
    @DubboReference
    private GridMemberService memberService;
    @DubboReference
    private GridBindingService gridBindingService;
    @DubboReference
    private GridMemberGridBindingService gridMemberGridBindingService;

    @DubboReference
    private HouseholdService householdService;

    /**
     * 查询网格员是否有该住户权限
     *
     * @param tenantId
     * @param gridMemberId
     * @param householdId
     * @return
     */
    public Boolean checkGridMemberHouseholdResource(Long tenantId, Long gridMemberId, Long householdId) {
        Map<String, Object> params = new HashMap<>();
        params.put(LambdaUtil.getFieldName(HouseholdFamilyDto::getTenantId), tenantId);
        List<Long> sourceIds = getSourceIdByMemberIdAndSourceType(gridMemberId, GridConstant.SourceType.BUILDING.getType());
        params.put(HouseholdConstants.BUILDING_IDS, sourceIds);
        params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(householdId));
        //如果查询不到该住户,证明没有权限
        if (householdService.queryTotal(params) <= 0) {
            return Boolean.FALSE;
        } else {
            return Boolean.TRUE;
        }
    }


    /**
     * 通过网格id 精准获取网格下的资源id
     */
    public List<Long> getSourceIdByGridIdAndSourceType(Long gridId, Byte sourceType) {
        List<Long> sourceIds = new ArrayList<>();
        List<GridBindingDto> bindingDtoList = gridBindingService.selectListByGridIdAndSourceType(gridId, sourceType);
        if (CollectionUtils.isNotEmpty(bindingDtoList)) {
            sourceIds = bindingDtoList.stream()
                    .map(GridBindingDto::getSourceId).collect(Collectors.toList());
        }
        return sourceIds;
    }


    /**
     * 通过floorid 或者 单位id 获取网格名
     *
     * @return
     */
    public String getGridName(Byte sourceType, Long sourceId) {
        String name = null;
        GridBindingDto gridBinding = gridBindingService.selectOne(sourceType, sourceId);
        if (gridBinding != null) {
            Long gridId = gridBinding.getGridId();
            GridDto grid = gridService.selectById(gridId);
            if (grid != null) {
                name = grid.getName();
            }
        }
        return name;
    }

    /**
     * 通过网格员和类型
     * 返回资源类型id list
     *
     * @param gridMemberId 网格员ID
     * @param sourceType
     * @return
     */
    public List<Long> getSourceIdByMemberIdAndSourceType(Long gridMemberId, Byte sourceType) {

        Set<Long> recordSet = new HashSet<>();
        // 根据网格员查询绑定网格列表
        List<GridMemberGridBindingDto> memberGridBindingList =
                gridMemberGridBindingService.selectListByMemberId(gridMemberId);
        if (CollectionUtils.isNotEmpty(memberGridBindingList)) {
            for (GridMemberGridBindingDto memberGridBinding : memberGridBindingList) {
                // 根据网格查询和类型  查询资源绑定列表
                List<GridBindingDto> bindingDtoList
                        = gridBindingService.selectListByGridIdAndSourceType(memberGridBinding.getGridId(), sourceType);
                for (GridBindingDto gridBinding : bindingDtoList) {
                    recordSet.add(gridBinding.getSourceId());
                }
            }
        }
        return new ArrayList<>(recordSet);
    }


    /**
     * 根据网格员获取其拥有的网格list
     *
     * @param gridMember
     * @return
     */
    public List<Long> getGridIdByGridMember(GridMemberDto gridMember) {
        List<Long> gridIds = new ArrayList<>();
        // 根据网格员查询绑定网格列表
        List<GridMemberGridBindingDto> memberGridBindingList =
                gridMemberGridBindingService.selectListByMemberId(gridMember.getId());
        if (CollectionUtils.isNotEmpty(memberGridBindingList)) {
            Set<Long> gridIdSet = memberGridBindingList.stream().map(GridMemberGridBindingDto::getGridId).collect(Collectors.toSet());
            gridIds = new ArrayList<>(gridIdSet);
        }
        return gridIds;
    }
}














